package com.seocoo.worker.order.model;


import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.seocoo.worker.base.http.BasePageRsp;
import com.seocoo.worker.base.http.BaseRsp;
import com.seocoo.worker.order.entity.RspOrderIWorkmateInfo;
import com.seocoo.worker.order.req.ReqAddWorkers;
import com.seocoo.worker.order.req.ReqRemoveWorker;
import com.seocoo.worker.order.req.ReqWorkersList;
import com.seocoo.worker.order.req.ReqWorkersRelevanced;
import com.seocoo.worker.user.model.request.ReqRefreshLocation;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.HandleException;
import com.sfh.lib.IResult;
import com.sfh.lib.mvvm.BaseViewModel;

import java.util.List;

/**
 * author : Ssp
 * e-mail : 981297079@qq.com
 * date   : 2020/2/2320:32
 * desc   : 关联工友
 * version: 1.0
 * (String locationLat, String locationLon, int pageNum, String phoneNumber)
 */
public class OrderRelevanceWorkmateModel extends BaseViewModel implements AMapLocationListener {

    AMapLocationClient locationClient;

    int pageNum;
    String phoneNumber;

    public void queryFriendList(int pageNum, String phoneNumber) {
        this.phoneNumber = phoneNumber;
        this.pageNum = pageNum;
        if (locationClient == null) {
            locationClient = getSingleLocationClient(AppCacheManager.getApplication(), this);
        }
        locationClient.stopLocation();
        locationClient.startLocation();
    }


    @Override
    public void onLocationChanged(AMapLocation amapLocation) {

        String locationLat = "";
        String locationLon = "";
        if (amapLocation.getErrorCode() == 0) {
            //可在其中解析amapLocation获取相应内容。

            locationLat = String.valueOf(amapLocation.getLatitude());
            locationLon = String.valueOf(amapLocation.getLongitude());
            //更新工匠的位置信息
            execute(new ReqRefreshLocation(amapLocation.getLatitude(), amapLocation.getLongitude()));

            Log.d("TAG", "onLocationChanged: " + amapLocation);
        } else {
            //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
            Log.e("AmapError", "location Error, ErrCode:"
                    + amapLocation.getErrorCode() + ", errInfo:"
                    + amapLocation.getErrorInfo());
        }
        ReqWorkersList reqWorkersList = new ReqWorkersList();
        reqWorkersList.setPageNum(pageNum);
        reqWorkersList.setPhoneNumber(phoneNumber);
        reqWorkersList.setLocationLat(locationLat);
        reqWorkersList.setLocationLon(locationLon);
        this.execute(reqWorkersList, new IResult<BasePageRsp<RspOrderIWorkmateInfo>>() {
            @Override
            public void onFail(HandleException e) {
                setValue("onFriendList");
            }

            @Override
            public void onSuccess(BasePageRsp<RspOrderIWorkmateInfo> friendBasePageRsp) throws Exception {
                setValue("onFriendList", friendBasePageRsp);
            }
        });

    }

    /***
     * 单次定位
     * @param context
     * @param listener
     * @return 可能返回null
     */
    public AMapLocationClient getSingleLocationClient(Context context, AMapLocationListener listener) {

        if (context == null) {
            return null;
        }
        AMapLocationClient mLocationClient = new AMapLocationClient(context.getApplicationContext());
        //初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位模式为Hight_Accuracy高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);

        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.setLocationListener(listener);
        mLocationClient.startLocation();
        return mLocationClient;

    }

    public void addWorks(ReqAddWorkers reqAddWorkers) {
        this.execute(reqAddWorkers, baseRsp -> {
            if (baseRsp.success()) {
                setValue("onAddWorksSuccess");
            }
        });

    }

    public void removeWorks(ReqRemoveWorker reqAddWorkers, int position) {
        this.execute(false, reqAddWorkers, baseRsp -> {
            hideLoading();
            if (baseRsp.success()) {
                setValue("onRemoveWorksSuccess", position);
            }
        });

    }

    public void queryWorker(String installId) {
        ReqWorkersRelevanced reqWorkersList = new ReqWorkersRelevanced(installId);
        this.execute(true,reqWorkersList, new IResult<BaseRsp<List<RspOrderIWorkmateInfo>>>() {
            @Override
            public void onFail(HandleException e) {
                hideLoading();
                setValue("onFriendList");
            }

            @Override
            public void onSuccess(BaseRsp<List<RspOrderIWorkmateInfo>> friendBasePageRsp) throws Exception {
                hideLoading();
                setValue("onFriendList", friendBasePageRsp);
            }
        });

    }


}
